import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

sns.set()
np.random.seed(0)
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
plt.show()

# #===================================例2： 首先造一张数据表====================================
region = ['Azerbaijan', 'Bahamas', 'Bangladesh', 'Belize', 'Bhutan',
          'Cambodia', 'Cameroon', 'Cape Verde', 'Chile', 'China']
kind = ['Afforestation & reforestation', 'Biofuels', 'Biogas', 'Biomass', 'Cement']
np.random.seed(20180316)  # 原来每次运行代码时设置相同的seed，则每次生成的随机数也相同，如果不设置seed，则每次生成的随机数都会不一样
arr_region = np.random.choice(region, size=(200,))
'''
numpy.random.choice(a, size=None, replace=True, p=None)
 a:一维数组或者一个整数
    如果是ndarray，则从数组元素生成随机样本。
    如果是整数，则随机样本的生成是np .arange(n)
 size：可选，整型或者tuple形式
 replace：布尔型，表示样本是否有更换
 p：一维数组类型，可选。表示从a 中以概率P，随机选择。如果没有给定，样本假设a中的所有项的分布是均匀的。
'''
list_region = list(arr_region)
arr_kind = np.random.choice(kind, size=(200,))
list_kind = list(arr_kind)

values = np.random.randint(100, 200, 200)
list_values = list(values)
df = pd.DataFrame({'region': list_region, 'kind': list_kind, 'values': list_values})
print(df)
'''
# <class 'numpy.ndarray'>
#          region                           kind  values
# 0      Cameroon                         Cement     140
# 1        Bhutan  Afforestation & reforestation     178
# 2         China                       Biofuels     161
# 3      Cambodia  Afforestation & reforestation     100
# 4       Bahamas                       Biofuels     129
# 5    Azerbaijan                         Cement     121
# 6         China  Afforestation & reforestation     142
# 7    Azerbaijan  Afforestation & reforestation     157
# 8       Bahamas  Afforestation & reforestation     134
# 9        Belize                         Cement     125
# 10     Cameroon                         Biogas     177
# 11   Bangladesh                        Biomass     163
# 12   Bangladesh                         Biogas     127
# 13   Bangladesh                         Cement     128
# 14      Bahamas                         Cement     181
# 15     Cambodia                        Biomass     105
# 16     Cameroon                         Cement     193
# 17   Cape Verde                         Cement     160
# 18      Bahamas                       Biofuels     181
# 19       Belize                        Biomass     117
# 20      Bahamas                         Biogas     165
# 21   Cape Verde                         Biogas     183
# 22      Bahamas                        Biomass     153
# 23     Cambodia                         Cement     134
# 24       Bhutan                        Biomass     199
# 25       Belize                         Biogas     123
# 26       Belize                         Cement     173
# 27     Cameroon                         Biogas     182
# 28   Azerbaijan                       Biofuels     177
# 29      Bahamas  Afforestation & reforestation     109
# ..          ...                            ...     ...
# 170    Cambodia                         Cement     163
# 171    Cambodia                        Biomass     107
# 172      Bhutan                        Biomass     185
# 173      Belize                         Cement     151
# 174    Cameroon                         Cement     183
# 175  Bangladesh                       Biofuels     192
# 176      Bhutan                         Cement     133
# 177     Bahamas                       Biofuels     126
# 178  Azerbaijan                        Biomass     179
# 179  Bangladesh                         Cement     127
# 180       China                        Biomass     181
# 181    Cameroon                         Cement     138
# 182       Chile                       Biofuels     175
# 183  Cape Verde                         Biogas     176
# 184    Cambodia                         Cement     192
# 185  Cape Verde  Afforestation & reforestation     155
# 186  Cape Verde                       Biofuels     188
# 187    Cambodia                         Biogas     139
# 188    Cambodia                         Biogas     191
# 189    Cameroon                         Cement     135
# 190  Cape Verde                         Biogas     134
# 191  Bangladesh                         Biogas     174
# 192      Belize                        Biomass     174
# 193    Cameroon                        Biomass     153
# 194  Azerbaijan                        Biomass     177
# 195      Belize                       Biofuels     119
# 196       China                       Biofuels     183
# 197    Cameroon                         Biogas     114
# 198       China  Afforestation & reforestation     143
# 199      Belize  Afforestation & reforestation     149
#
# [200 rows x 3 columns]
#
# '''
print(df['kind'].value_counts())
'''
Cement                           47
Biogas                           44
Biofuels                         41
Afforestation & reforestation    38
Biomass                          30
Name: kind, dtype: int64
'''
# 将DataFrame数据表转换成“数据透视表”
pt = df.pivot_table(index='kind', columns='region', values='values', aggfunc=np.sum)
print(pt)
# index是行，columns是列，values是表中展示的数据，aggfunc是表中展示每组数据使用的运算
'''
region                         Azerbaijan  Bahamas  ...    Chile  China
kind                                                ...
Afforestation & reforestation         568      571  ...      225    608
Biofuels                              515      903  ...      313    782
Biogas                                499      614  ...      715    130
Biomass                               834      153  ...      164    749
Cement                                431      549  ...      194    747
[5 rows x 10 columns]
'''

# ======================================center的用法（颜色）==================================
f, (ax1, ax2) = plt.subplots(figsize=(6, 4), nrows=2)
cmap = sns.cubehelix_palette(start=1.5, rot=3, gamma=0.8, as_cmap=True)  # 从数字到色彩空间的映射
sns.heatmap(pt, linewidths=0.05, ax=ax1, cmap=cmap, center=None)
ax1.set_title('center=None')
ax1.set_xlabel('')
ax1.set_xticklabels([])  # 设置x轴图例为空值
ax1.set_ylabel('kind')

# 当center设置小于数据的均值时，生成的图片颜色要向0值代表的颜色一段偏移
sns.heatmap(pt, linewidths=0.05, ax=ax2, cmap=cmap, center=200)
ax2.set_title('center=3000')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')
plt.show()